import { defineStore } from "pinia";
import { listFormFieldsRequest } from "@/api/project/form";

interface ConditionsConfig {
  id?: string;
  value?: any;
  index?: any;
  flag?: boolean;
  conditionNodes?: Array<object>;
}

interface User {
  id: number;
  nickName: string;
  userName: string;
}

interface FormProperty {
  id: string;
  name: string;
  readable: boolean;
  writeable: boolean;
}

interface TaskEvents {
  agree: boolean;
  reject: boolean;
  agreeNotifyUrl: string;
  rejectNotifyUrl: string;
}

interface Node {
  type: number;
  jobType: string;
  settype: number;
  nextNode: Node | null;
  nodeName: string;
  nodeType: string;
}

interface ApproverConfig extends Node {
  id?: number;
  value?: any;
  taskEvents: TaskEvents;
  adviceStatus: number;
  nodePostList: any[];
  nodeRoleList: any[];
  nodeUserList: User[];
  signPadStatus: number;
  formProperties: FormProperty[];
  formUserFields: any | null;
  approveDirectorLevel: any | null;
  multipleApprovalType: string;
  operationBtnList?: string[];
}

interface PromoterConfig {
  formProperties?: FormProperty[];
}

// 工作流配置
interface FormWorkflowState {
  // 发起人抽提开关
  promoterDrawer: boolean;
  // 发起人配置
  promoterConfig: PromoterConfig;
  // 审批人抽屉开关
  approverDrawer: boolean;
  // 审批人配置
  approverConfig: ApproverConfig | {};
  // 抄送人抽屉开关
  copyerDrawer: boolean;
  // 抄送人配置
  copyerConfig: object;
  // 条件抽屉开关
  conditionDrawer: boolean;
  // 条件配置
  conditionsConfig: ConditionsConfig;
  // 表单的字段
  formFields: Array<any>;
  // 显示消息配置模板
  showMsgConfigTemplate: boolean;
}

/**
 * 表单流程
 * @methods
 */
export const useFormWorkflow = defineStore("formWorkflow", {
  state: (): FormWorkflowState => ({
    promoterDrawer: false,
    promoterConfig: {},
    approverDrawer: false,
    approverConfig: {},
    copyerDrawer: false,
    copyerConfig: {},
    conditionDrawer: false,
    conditionsConfig: {
      conditionNodes: []
    },
    formFields: [],
    showMsgConfigTemplate: false
  }),
  actions: {
    async updatePromoter() {
      this.promoterDrawer = !this.promoterDrawer;
    },
    async updatePromoterConfig(config: PromoterConfig) {
      this.promoterConfig = config;
    },
    async updateApprover() {
      this.approverDrawer = !this.approverDrawer;
    },
    async updateCopyer() {
      this.copyerDrawer = !this.copyerDrawer;
    },
    async updateCondition() {
      this.conditionDrawer = !this.conditionDrawer;
    },
    async updateApproverConfig(config: object) {
      this.approverConfig = config;
    },
    async updateCopyerConfig(config: object) {
      this.copyerConfig = config;
    },
    async updateConditionsConfig(config: ConditionsConfig) {
      this.conditionsConfig = config;
    },
    async updateConditionsConfigConditionNodes(config: Array<object>) {
      this.conditionsConfig.conditionNodes = config;
    },
    async loadFormAllFields(formKey: string) {
      const res = await listFormFieldsRequest(formKey, true);
      this.formFields = res.data?.filter(item => item.type !== "PAGINATION");
    }
  }
});
